﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.IO;

namespace MovieDB
{
    /// <summary>
    /// Sämtliche Optionen werden in der Registrierung gespeichert und über diese Klasse
    /// bereitgestellt und auch gespeichert
    /// </summary>
    public class MdbOptions
    {
        private string dbDirectory = string.Empty;
        private string tmdbApiKey = string.Empty;
        private List<string> movieFolders = new List<string>();

        private bool useImdbIdFromXml = true;
        private bool loadPoster = true;
        private bool loadFanart = true;
        private bool loadXml = true;

        private bool critical = false;

        RegistryKey MovieDbKey;

        /// <summary>
        /// Pfad zum Datenbankverzeichnis
        /// </summary>
        public string DbDirectory
        {
            get
            {
                if (this.dbDirectory == null)
                {
                    return string.Empty;
                }
                else
                {
                    return this.dbDirectory;
                }
            }

            set
            {
                this.dbDirectory = value;
                MovieDbKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\MovieDb\\App");
                MovieDbKey.SetValue("dbDirectory", value);
            }
        }

        /// <summary>
        /// Der tmdb API Key, der für Abfragen genutzt wird
        /// </summary>
        public string TmdbApiKey
        {
            get
            {
                return this.tmdbApiKey;
            }

            set
            {
                this.tmdbApiKey = value;
                MovieDbKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\MovieDb\\App");
                MovieDbKey.SetValue("apiKey", value);
            }
        }

        /// <summary>
        /// Gibt an, ob wichtige Informationen fehlen
        /// </summary>
        public bool MissingCriticalInformation
        {
            get
            {
                return this.critical;
            }
        }

        /// <summary>
        /// Liste von Film Verzeichnissen
        /// </summary>
        public List<string> MovieFolders
        {
            get { return this.movieFolders; }
        }

        /// <summary>
        /// Der Konstruktor liest die vorhandenen Werte aus der Registrierung aus
        /// </summary>
        public MdbOptions()
        {
            // Pfad zum Datenbankverzeichnis einlesen
            if (!SubKeyExist("SOFTWARE\\MovieDb\\App"))
            {
                MovieDbKey = Registry.CurrentUser.CreateSubKey("SOFTWARE\\MovieDb\\App");
            }
            else
            {
	            MovieDbKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\MovieDb\\App");
            }
	            
            this.dbDirectory = (string)MovieDbKey.GetValue("dbDirectory");
            this.tmdbApiKey = (string)MovieDbKey.GetValue("apiKey");
            string mf = (string)MovieDbKey.GetValue("movieFolders");

            if (this.dbDirectory == null)
            {
                MovieDbKey.SetValue("dbDirectory", "");
                this.critical = true;
            }
            else
            {
                if (!Directory.Exists(this.dbDirectory))
                {
                    this.critical = true;
                }
            }

            if (this.tmdbApiKey == null)
            {
                MovieDbKey.SetValue("apiKey", "");
                //this.critical = true;
            }

            if (mf != null)
            {
                foreach (string folder in mf.Split(','))
                {
                    this.movieFolders.Add(folder);
                }
            }
            else
            {
                MovieDbKey.SetValue("movieFolders", "");
                this.critical = true;
            }
        }
	
        /// <summary>
        /// Prüft, ob ein bestimmter Key in der Registry existiert
        /// </summary>
        /// <param name="Subkey">Der zu prüfende Key</param>
        /// <returns>Angabe als booleschen Wert</returns>
        private bool SubKeyExist(string Subkey)
        {
	        RegistryKey myKey = Registry.CurrentUser.OpenSubKey(Subkey);
	        if (myKey == null)
		        return false;
	        else
		        return true;
        }

        public void SaveMovieFolders()
        {
            StringBuilder regVal = new StringBuilder();

            foreach (string folder in this.movieFolders)
            {
                regVal.Append(folder + ',');
            }

            MovieDbKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\MovieDb\\App");
            MovieDbKey.SetValue("movieFolders", regVal.ToString().Substring(0, regVal.Length));
        }
    }
}
